<template>
  <div>
    <div>内容管理</div>
    <div class="headAdministration">广告图管理/内容管理</div>
    <div class="headTime">
      <span>名称：</span
      ><el-input v-model="name" placeholder="请输入内容"></el-input>
      <span>类型：</span>
      <el-select v-model="type" placeholder="请选择">
        <el-option
          v-for="item in types"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <span>状态：</span>
      <el-select v-model="status" placeholder="请选择">
        <el-option
          v-for="item in statuses"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <button class="choose" @click="choose">筛选</button>
      <button class="add" @click="add">添加</button>
    </div>
    <template>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column label="图片" width="300">
          <template slot-scope="scope">
            <img
              class="imgUrl"
              style="margin-left: 10px; height: 100px; width: 200px"
              :src="scope.row.url"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="adType.name"
          label="类型"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="time"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="orders" label="排序"> </el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 7, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加模态框 -->
    <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false">
      <div class="addBox">
        <div class="addHead">添加</div>
        <p>标题:</p>
        <el-input v-model="addList.name" placeholder="请输入内容"></el-input>
        <p class="headWord">类型：</p>
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <p>图片</p>
        <el-upload
          class="avatar-uploader"
          action="http://192.168.31.39:9101/ad/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <p>排序:</p>
        <el-input v-model="addList.orders" placeholder="请输入内容"></el-input>
        <p>状态:</p>
        <el-radio v-model="addList.status" label="启用">启用</el-radio>
        <el-radio v-model="addList.status" label="禁用">禁用</el-radio>
        <div class="btn">
          <el-button type="primary" @click="putTrue">确认</el-button>
          <el-button type="info" @click="FalseT">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      currentPage: 1,
      pagesize: 2,
      list: [],
      total: 3,
      drawer: false,
      drawerAdd: true,
      addList: {},
      options: [
        {
          value: "游戏类",
          label: "游戏类",
        },
        {
          value: "生活类",
          label: "生活类",
        },
        {
          value: "健康类",
          label: "健康类",
        },
      ],
      types: [
        {
          value: "游戏类",
          label: "游戏类",
        },
        {
          value: "生活类",
          label: "生活类",
        },
        {
          value: "健康类",
          label: "健康类",
        },
      ],
      statuses: [
        {
          value: "启用",
          label: "启用",
        },
        {
          value: "禁用",
          label: "禁用",
        },
      ],
      value: "",
      imageUrl: "",
      type: "",
      status: "",
      name: "",
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    open2(msg) {
      this.$message({
        message: `${msg}成功`,
        type: "success",
      });
    },
    handleDelete(index, row) {
      console.log(index, row.adId);
        axios
        .get(`http://192.168.31.39:9101/ad/del/${row.adId}`)
        .then((res) => {
          if (res.data.code == 200) {
            this.open2("删除");
            this.getList(this.currentPage, this.pagesize);
          }
        });
    },
    getList(currentPage, pagesize) {
      axios
        .get(`http://192.168.31.39:9101/ad/findAll/${currentPage}/${pagesize}`)
        .then((res) => {
          console.log(res);
          // this.total = res.data.data.total;
          this.list = res.data.data.data;
          this.total = res.data.data.total;
          this.list.map((item) => {
            if (item.status == 1) {
              item.status = "启用";
            } else {
              item.status = "禁用";
            }
          });
        });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.getList(this.currentPage, this.pagesize);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList(this.currentPage, this.pagesize);
    },
    //确定修改
    putTrue() {
      // this.drawer = false;
      // this.drawerAdd = true;
      this.addList["type"] = this.value;
      let list = { ...this.addList };
      if (this.list.status == "启用") {
        list.status = 1;
      } else {
        list.status = 0;
      }
      console.log("addList", list);
      // if (this.drawerAdd) {
      //   this.editList(list);
      // } else {
      //   this.addT(list);
      // }
      this.drawer = false;
    },
    FalseT() {
      this.drawer = false;
      this.drawerAdd = true;
    },
    add() {
      this.drawer = true;
      // console.log("tianjai ");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log("this.imageUrl", this.imageUrl);
    },
    beforeAvatarUpload(file) {
      console.log("this.imageUrlthis.imageUrlthis.imageUrl");
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      console.log(isJPG, isLt2M, file);
      return isJPG && isLt2M && file;
    },
    //筛选
    choose() {
      let list = { title: this.name, typeId: this.type, status: this.status };
      if (list.status == "启用") {
        list.status = 1;
      } else {
        list.status = 0;
      }
       axios({
        url: "http://192.168.31.39:9101/ad/findByTypeAndStatus",
        method: "post",
        data: list,
      }).then((res) => {
        console.log("saixuan",res);
        // if (res.data.code == 200) {
        //   this.list = res.data.data;
        //   this.type = "";
        //   this.value = "";
        // }
      });
      // console.log(list);
    },
  },
  created() {
    this.getList(this.currentPage, this.pagesize);
  },
};
</script>
<style scoped>
.choose {
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 50px;
  margin-left: 20px;
  border-radius: 3px;
}
.headAdministration {
  margin: 10px 0;
}
.headTime {
  margin-bottom: 20px;
  position: relative;
}
.add {
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 60px;
  position: absolute;
  right: 0px;
  border-radius: 3px;
}
.addHead {
  text-align: center;
}
.addBox {
  padding: 20px 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.btn {
  margin-top: 20px;
  text-align: center;
}
.imgUrl {
  width: 200px;
  height: 100px;
}
.el-input {
  width: 200px;
}
</style>